<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .input-box {
            margin: 10px;
            display: flex;
        }

        .input-box input {
            flex: 1;
            height: 40px;
            border: 1px solid #ccc;
            outline: none;
            padding-left: 20px;
            border-radius: 5px;
        }

        span {
            text-align: center;
            color: #ade;
            width: 60px;
        }

        .list {
            /* background-color: aqua; */
            margin: 10px;
        }

        .task {
            margin: 20px 0;
            display: flex;
            padding: 15px;
            justify-content: space-around;
        }

        .empty {
            text-align: center;
            margin: 50px;
        }

        .list .task.finished {
            background-color: lightseagreen;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="input-box">
            <input type="text" placeholder="输入任务名称" v-model="inputText"><span @click="add">添加</span>
        </div>
        <div class="list">
            <div :class=" `task ${item.status?'finished':''}` " v-for="(item,index) in tasklist" :key="index"></div>
            <!-- <div :class="{task:true,finished:item.status}" v-for="(item,index) in tasklist" :key="index"> -->
            <input type="checkbox" @click="changStatus(item)">
            <div class="name">{{item.name}}</div>
            <span class="btn">删除</span>
        </div>
        <div class="empty" v-show="tasklist.length==0">空空如也</div>
    </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: "#app",
        data: {
            inputText: '',//保存输入框的值
            tasklist: [
                // {id:1,name:'任务名称',status:true}
            ]
        },
        methods: {//输入框不为空，才能添加任务
            add() {
                if (this.inputText) {//输入框不为空
                    //构造一个任务对象
                    var obj = { id: this.tasklist.length + 1, name: this.inputText, status: false };
                    //将任务对象添加到任务列表中
                    this.tasklist.push(obj);
                    //清空输入框
                    this.inputText = '';
                }
            },
            changStatus(item) {
                item.status = !item.status
            }
        }

    })
</script>